package com.diy.sigmund.diycommon.util;

import com.diy.sigmund.diycommon.entity.ResultMsg;
import com.diy.sigmund.diycommon.entity.base.exception.DiyRuntimeException;
import com.diy.sigmund.diycommon.entity.enumerate.ResponseEnum;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import org.apache.commons.lang3.StringUtils;

/**
 * 不需要自定义函数式接口，处理 if else 分支
 *
 * @author ylm-sigmund
 * @since 2021/12/16 9:35
 */
public class IfElseUtil {

    /**
     * 抛出异常
     *
     * @param value value
     * @return Consumer
     */
    public static Consumer<ResponseEnum> throwDiyRuntimeException(boolean value) {
        return responseEnum -> {
            if (value) {
                throw new DiyRuntimeException(responseEnum);
            }
        };
    }

    public static BiConsumer<Runnable, Runnable> trueOrFalseRR(boolean value) {
        return (trueRunnable, falseRunnable) -> {
            if (value) {
                trueRunnable.run();
            } else {
                falseRunnable.run();
            }
        };
    }

    public static BiConsumer<Runnable, Consumer<String>> trueOrFalserRC(String msg) {
        return (trueRunnable, consumer) -> {
            if (StringUtils.isBlank(msg)) {
                trueRunnable.run();
            } else {
                consumer.accept(msg);
            }
        };
    }

    @Deprecated
    public static Function<ResponseEnum, ResultMsg> returnFail(boolean value) {
        return responseEnum -> {
            if (value) {
                return ResultMsg.fail(responseEnum);
            }
            return null;
        };
    }

    public static void main(String[] args) {
        // IfElseUtil.throwDiyRuntimeException(true).accept(ResponseEnum.ILLEGAL_REQUEST);
        // ResultMsg apply = IfElseUtil.returnFail(true).apply(ResponseEnum.ILLEGAL_REQUEST);
        IfElseUtil.trueOrFalseRR(false).accept(() -> System.out.println("我是成功分支"),
                () -> System.out.println("我是失败分支"));
        IfElseUtil.trueOrFalserRC("3333").accept(() -> System.out.println("空字符串"), System.out::println);
        IfElseUtil.trueOrFalserRC("").accept(() -> System.out.println("空字符串"), System.out::println);
        System.out.println(1);
    }
}
